<!DOCTYPE html>
<html class="x-admin-sm">

	<head>
		<meta charset="UTF-8">
		<title>欢迎页面-X-admin2.2</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
		<link rel="stylesheet" href="./css/font.css">
		<link rel="stylesheet" href="./css/xadmin.css">
		<script src="./lib/layui/layui.js" charset="utf-8"></script>
		<script type="text/javascript" src="./js/xadmin.js"></script>
		<script src="./js/global.js" type="text/javascript" charset="utf-8"></script>
		<!--[if lt IE 9]>
          <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
          <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
	</head>  

	<body>
		 <div class="x-nav">
            <span class="layui-breadcrumb">
                
            </span>
            <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
                <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
            </a>
        </div>
		<div class="layui-fluid">
			<div class="layui-row layui-col-space15">
				<div class="layui-col-md12">
					<div class="layui-card">
						<div class="layui-card-body ">
							<div class="layui-row">

								<div class="layui-col-md2">
									<div id="divModules" class="demo-tree demo-tree-box" style="width: 100%; height:100%; ">
									</div>
								</div>

								<div class="layui-col-md10">
									<div id="divModuleB">
										<button id="btnAddChild" type="button" class="layui-btn layui-btn-sm">添加子节点</button>
										<button id="btnChangeParent" type="button" class="layui-btn layui-btn-normal layui-btn-sm">设置父节点</button>
										<fieldset class="layui-elem-field layui-field-title" style="margin-top: 10px;">
											<legend><span id="spTitle">功能模块信息维护</span></legend>
										</fieldset>
										<form id="formSave" class="layui-form" action="">
											<div class="layui-form-item">
												<label class="layui-form-label">父节点编号</label>
												<div class="layui-input-block">
													<input id="txtParentId" type="text" disabled="disabled" value="0" class="layui-input">
													<input id="hfParentId" type="hidden" name="parentId">
												</div>
											</div>
											<div class="layui-form-item">
												<label class="layui-form-label">节点编号</label>
												<div class="layui-input-block">
													<input id="hfId" type="hidden" name="mId">
													<input id="txtId" type="text" disabled="disabled" value="0" class="layui-input">
												</div>
											</div>
											<div class="layui-form-item">
												<label class="layui-form-label">节点名称</label>
												<div class="layui-input-block">
													<input id="txtName" type="text" name="name" lay-verify="required" autocomplete="off" placeholder="请输入节点名称" class="layui-input">
												</div>
											</div>
											<div class="layui-form-item">
												<label class="layui-form-label">路径</label>
												<div class="layui-input-block">
													<input id="txtPath" type="text" name="path" placeholder="请输入功能路径" autocomplete="off" class="layui-input">
												</div>
											</div>

											<div class="layui-form-item">
												<label class="layui-form-label">权重</label>
												<div class="layui-input-block">
													<input id="txtWeight" type="text" name="weight" lay-verify="required|number" placeholder="请输入权重(整数)" autocomplete="off" class="layui-input">
												</div>
											</div>

											<div class="layui-form-item">
												<div class="layui-input-block">
													<button class="layui-btn" lay-submit="" lay-filter="subData">保存</button>
													<button type="reset" class="layui-btn layui-btn-primary">重置</button>
												</div>
											</div>
										</form>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
	</body>
	<script>
		var treeNode = null;
		var moo = null;
		layui.use(['tree', 'layer', 'form'], function() {
			var tree = layui.tree;
			var layer = layui.layer;
			var $ = layui.$;
			var form = layui.form;

			 $("#del").hide();

			form.on('submit(subData)', function(data) {
			var ix = layer.load(0, {
				shade: false
			});
			//添加子节点操作
			if($("#hfId").val() == "0") {
				var pid=0;
				if($("#hfParentId").val()=="" || $("#hfParentId").val()==null){
					pid=0;
				}else{
					pid=$("#hfParentId").val();
				}
				$.post(globalData.server+"module/getSave", {"title": $("#txtName").val(),
						"href": $("#txtPath").val(),
						"weight": $("#txtWeight").val(),
						"parentId": $("#txtParentId").val(),"token":globalData.getCurToken()},
				function(res) {
					if(res.success) {
							layer.msg("添加成功");
							window.location.reload();
						} else
							layer.msg("添加失败，节点名重复");
						layer.close(ix);
				}, "json");
			} else { //更新节点或调整父节点
				$("#hfParentId").val($("#txtParentId").val());
					var title = $("#txtName").val();
					var str = $("#formSave").serialize() + "&token=" + globalData.getCurToken();
					$.post(globalData.server + "module/getSave", {
						"id": $("#txtId").val(),
						"title": $("#txtName").val(),
						"href": $("#txtPath").val(),
						"weight": $("#txtWeight").val(),
						"parentId": $("#txtParentId").val(),
						"token": globalData.getCurToken()
					}, function(res) {
						if(res.success) {
							layer.msg("保存成功");
							$("#txtParentId").attr("disabled", "disabled");
							$("#txtId").attr("disabled", "disabled");
							treeNode.data.title = title;
							treeNode.elem.find('.layui-tree-txt').html(title);
							window.location.reload();
						} else
							layer.msg(res.message);
						layer.close(ix);
					}, "json");
			}
			return false;
		}); 
		$(function() {
			$("#btnAddChild").click(function() {
				var id = $("#hfId").val();
					var name = $("#txtName").val();
					$("#spTitle").html("为【" + name + "】添加子节点");
					$("#hfId").val("0");
					$("#txtId").val("0");
					$("#hfParentId").val(id);
					$("#txtParentId").val(id);
					$("#txtName").val("");
					$("#txtPath").val("");
					$("#txtWeight").val("");
			});
			$("#btnChangeParent").click(function() {
				var id = $("#hfId").val();
				var name = $("#txtName").val();
				$("#spTitle").html("为【" + name + "】调整父分类");
				$("#txtParentId").removeAttr("disabled");
			});
		})
		active = {
			reload: function() {
				$.post(globalData.server+"module/getAllM", {'token':globalData.getCurToken()}, function(res) {
					tree.reload('tv', {
						data: res, 
					});
				}, "json"); 
			},
			load: function() { //加载当前页
				$.post(globalData.server+"module/getAllM", {'token':globalData.getCurToken()}, function(res) {
					tree.render({
						id: "tv",
						elem: "#divModules",
						accordion: true ,
						data: res,
						edit: ['del'],
						operate: function(obj) {

						},
						click: function(obj) {
							$("#spTitle").html("分类管理");
							treeNode = obj;
							var index = layer.load(0, {
								shade: false
							});
							$.post(globalData.server+"module/getById", {
								"id": obj.data.id,
								'token':globalData.getCurToken()
							}, function(res) {
								if(res.success) {
									console.log(res);
									$("#txtId").val(res.message.id)
									$("#hfId").val(res.message.id);
									$("#txtParentId").val(res.message.parentId);
									$("#hfParentId").val(res.message.parentId);
									$("#txtName").val(res.message.title);
									$("#txtPath").val(res.message.href);
									$("#txtWeight").val(res.message.weight);
								} else {
									layer.msg(res.message);
								} 
								layer.close(index);
							}, "json")
						},
						operate: function(obj){
							var type = obj.type; //得到操作类型：add、edit、del
						    var data = obj.data;  //得到当前节点的数据
						    console.log(data);

							if(type=="del"){
								
											if(data.children!=null){
									if(data.children.length==0){
										layer.confirm('确定要删除该分类吗？', function(index) {
										$.post(globalData.server+"module/delModules",{"Id":data.id,'token':globalData.getCurToken()},function(res){
											if(res.success){
												layer.msg("删除成功")
												active.load();
											}else{ 
												layer.msg("删除失败");
												active.load();
											}
										},"json"); 
									},function(){
										active.load();
									});
									}else{
										layer.msg("该分类下有子分类，无法删除！");
										active.load();
									}
								}else{
									layer.confirm('确定要删除该分类吗？', function(index) {
										$.post(globalData.server+"module/delModules",{"Id":data.id,'token':globalData.getCurToken()},function(res){
											if(res.success){
												layer.msg("删除成功")
												active.load();
											}else{ 
												layer.msg("删除失败");
												active.load();
											}
										},"json"); 
									},function(){
										active.load();
									});
								}
							}
						}

					});
				}, "json");
			}
		};
		active["load"].call(null);
	});
	</script>

</html>